package com.guomei.controller.curse;

import com.guomei.bean.curse.DistributionApply;
import com.guomei.bean.curse.GetByStatusDTO;
import com.guomei.bean.curse.ReviewApplyDTO;
import com.guomei.bean.curse.SubmitApply;
import com.guomei.service.curse.DistributionApplyService;
import com.guomei.utils.Result;
import com.guomei.utils.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 分销申请控制器
 */
@RestController
@RequestMapping("/api/distribution-apply")
@Slf4j
public class DistributionApplyController {
    @Resource
    private DistributionApplyService distributionApplyService;
    /**
     * 提交分销申请
     */
    @PostMapping("/submitApply")
    public Result<?> submitApply(@Valid @RequestBody SubmitApply submitApply, BindingResult result) {
        log.info("提交分销申请入参：{}", submitApply);
        //实体类注解参数校验
        Result commonDataResponse = ValidationUtil.validInputParams(result);
        if (commonDataResponse != null) {
            return commonDataResponse;
        }
        try {
            DistributionApply distributionApply = new DistributionApply();
            distributionApply.setCourseId(submitApply.getCourseId());
            distributionApply.setTeacherId(submitApply.getTeacherId());
            distributionApply.setDistributorId(submitApply.getDistributorId());
            distributionApply.setUserId(submitApply.getUserId());
            distributionApply.setDistributionRatio(submitApply.getDistributionRatio());
            return distributionApplyService.submitApply(distributionApply);
        } catch (Exception e) {
            log.info("提交分销申请异常：{}", e);
            return Result.error("提交分销申请异常");
        }
    }
    /**
     * 审核分销申请
     */
    @PostMapping("/review")
    public Result<?> reviewApply(@Valid @RequestBody ReviewApplyDTO reviewApplyDTO, BindingResult result) {
        log.info("审核分销申请入参：{}", reviewApplyDTO);
        //实体类注解参数校验
        Result commonDataResponse = ValidationUtil.validInputParams(result);
        if (commonDataResponse != null) {
            return commonDataResponse;
        }
        try {
            return distributionApplyService.reviewApply(reviewApplyDTO.getId(), reviewApplyDTO.getStatus(), reviewApplyDTO.getReviewRemark());
        } catch (Exception e) {
            log.info("审核分销申请异常：{}", e);
            return Result.error("审核分销申请异常");
        }
    }

    /**
     * 根据状态查询分销申请列表
     */
    @PostMapping("/select/status")
    public Result<?> getByStatus(@Valid @RequestBody GetByStatusDTO getByStatusDTO, BindingResult result) {
        log.info("根据状态查询分销申请列表入参：{}", getByStatusDTO);
        //实体类注解参数校验
        Result commonDataResponse = ValidationUtil.validInputParams(result);
        if (commonDataResponse != null) {
            return commonDataResponse;
        }
        try {
            if (getByStatusDTO == null || getByStatusDTO.getPageNum() == null || getByStatusDTO.getPageSize() == null){
                return Result.fail("分页字段信息缺失，查询失败");
            }
            // 验证分页参数是否有效
            if (getByStatusDTO.getPageNum() <= 0 || getByStatusDTO.getPageSize() <= 0) {
                return Result.fail("分页参数无效");
            }
            return distributionApplyService.getByStatus(getByStatusDTO);
        } catch (Exception e) {
            log.info("根据状态查询分销申请列表异常：{}", e);
            return Result.error("根据状态查询分销申请列表异常");
        }
    }
}
